package operation;

import book.Book;
import book.Bookshelf;

import java.sql.SQLOutput;
import java.util.Scanner;

public class AddOperation implements IOperation{
    public void work(Bookshelf bookshelf) {
        System.out.println("增加图书");
        int cur = bookshelf.getUsedSize();

        //看看书架是否已经满了
        if (cur == bookshelf.getBooks().length) {
            System.out.println("书架已满");
        } else {
            Book book = new Book();
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入书的名称：");
            book.setName(scanner.nextLine());;
            System.out.println("请输入书的作者：");
            book.setAuthor(scanner.nextLine());;
            System.out.println("请输入书的价格：");
            book.setPrice(scanner.nextInt());
            //注意这里面有一个 回车 我们要将其消化掉
            scanner.nextLine();

            System.out.println("请输入书的类型");
            book.setType(scanner.nextLine());

            //检查数组中有没有这本书
            for (int i = 0; i < cur; i++) {
                Book book1 = bookshelf.getBook(i);
                if (book.getName().equals(book1)) {
                    System.out.println("这本书已经有了");
                    return;
                }
            }
            bookshelf.setBooks(cur,book);
            bookshelf.setUsedSize(cur + 1);
            System.out.println("添加成功！");
        }

    }


}
